package com.education.platform.common.config;

import com.education.platform.common.redis.RedisUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.JedisPoolConfig;

/**
 * redis配置
 */
@Configuration
public class RedisConfiguration {

    @Value("${redis.host}")
    private String host;

    @Value("${redis.port}")
    private int port;

    @Value("${redis.timeout}")
    private int timeout;

    @Value("${redis.password}")
    private String password;

    @Value("${redis.database}")
    private int database;
    @Value("${redis.maxIdle}")
    private int maxIdle;
    @Value("${redis.maxTotal}")
    private int maxTotal;
    @Value("${redis.maxWaitMills}")
    private int maxWaitMills;
    @Value("${redis.minEvictableIdleTimeMillis}")
    private int minEvictableIdleTimeMillis;
    @Value("${redis.minIdle}")
    private int minIdle;
    @Value("${redis.softMinEvictableIdleTimeMillis}")
    private int softMinEvictableIdleTimeMillis;
    @Value("${redis.testOnBorrow}")
    private boolean testOnBorrow;
    @Value("${redis.testWhileIdle}")
    private boolean testWhileIdle;
    @Value("${redis.timeBetweenEvictionRunsMillis}")
    private int timeBetweenEvictionRunsMillis;
    @Value("${redis.blockWhenExhausted}")
    private boolean blockWhenExhausted;


    @Bean(name="redisUtils",destroyMethod = "destroy")
    public RedisUtils redisClientUtilsProperties(){
        JedisPoolConfig config = new JedisPoolConfig();
        config.setBlockWhenExhausted(blockWhenExhausted);
        config.setMaxIdle(maxIdle);
        config.setMaxTotal(maxTotal);
        config.setMaxWaitMillis(maxWaitMills);
        config.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
        config.setMinIdle(minIdle);
        config.setTestOnBorrow(testOnBorrow);
        config.setTestWhileIdle(testWhileIdle);
        config.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
        return RedisUtils.getInstance(config,host,port,timeout,password,database);

    }


}
